Page({
  data: {
    notifications: true,
    sound: true,
    vibration: true,
    cacheSize: '0KB',
    showLogoutConfirm: false
  },

  onLoad() {
    this.loadSettings();
    this.calculateCacheSize();
  },

  loadSettings() {
    const settings = wx.getStorageSync('settings') || {};
    this.setData({
      notifications: settings.notifications !== false,
      sound: settings.sound !== false,
      vibration: settings.vibration !== false
    });
  },

  calculateCacheSize() {
    wx.getStorageInfo({
      success: (res) => {
        const sizeInKB = (res.currentSize).toFixed(2);
        this.setData({
          cacheSize: `${sizeInKB}KB`
        });
      }
    });
  },

  saveSettings() {
    const { notifications, sound, vibration } = this.data;
    wx.setStorageSync('settings', {
      notifications,
      sound,
      vibration
    });
  },

  changePassword() {
    wx.navigateTo({
      url: '/pages/settings/change-password/index'
    });
  },

  changePhone() {
    wx.navigateTo({
      url: '/pages/settings/change-phone/index'
    });
  },

  deleteAccount() {
    wx.showModal({
      title: '账号注销',
      content: '注销账号后，所有数据将被永久删除，是否继续？',
      success: (res) => {
        if (res.confirm) {
          wx.request({
            url: 'https://api.example.com/account/delete',
            method: 'POST',
            success: (res) => {
              if (res.statusCode === 200) {
                wx.clearStorageSync();
                wx.reLaunch({
                  url: '/pages/login/index'
                });
              } else {
                wx.showToast({
                  title: '注销失败',
                  icon: 'none'
                });
              }
            },
            fail: () => {
              wx.showToast({
                title: '网络错误',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  },

  toggleNotifications(e) {
    this.setData({
      notifications: e.detail.value
    });
    this.saveSettings();
  },

  toggleSound(e) {
    this.setData({
      sound: e.detail.value
    });
    this.saveSettings();
  },

  toggleVibration(e) {
    this.setData({
      vibration: e.detail.value
    });
    this.saveSettings();
  },

  clearCache() {
    wx.showModal({
      title: '清除缓存',
      content: '确定要清除缓存吗？',
      success: (res) => {
        if (res.confirm) {
          wx.clearStorage({
            success: () => {
              wx.showToast({
                title: '清除成功',
                icon: 'success'
              });
              this.calculateCacheSize();
            }
          });
        }
      }
    });
  },

  checkUpdate() {
    const updateManager = wx.getUpdateManager();
    updateManager.onCheckForUpdate((res) => {
      if (res.hasUpdate) {
        wx.showModal({
          title: '更新提示',
          content: '发现新版本，是否重启应用？',
          success: (res) => {
            if (res.confirm) {
              updateManager.applyUpdate();
            }
          }
        });
      } else {
        wx.showToast({
          title: '已是最新版本',
          icon: 'success'
        });
      }
    });
  },

  aboutUs() {
    wx.navigateTo({
      url: '/pages/about/index'
    });
  },

  showLogoutDialog() {
    this.setData({ showLogoutConfirm: true });
  },

  hideLogoutDialog() {
    this.setData({ showLogoutConfirm: false });
  },

  handleLogout() {
    wx.clearStorageSync();
    wx.reLaunch({
      url: '/pages/login/index'
    });
  },

  goBack() {
    wx.navigateBack();
  }
}); 